#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_RxData;		//用来在中断里接收数据的变量
uint8_t Serial_RxFlag;		//接收数据标志位0/1
char Serial_RxPacket[100];	//接收文本数据包数组

/**
  * @brief	串口初始化函数
  * @param	无
  * @retval	无
  */
void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode  = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);	
}

/**
  * @brief	串口发送一个字节数据
  * @param	需要发送的一个字节数据
  * @retval	无
  */
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

/**
  * @brief	串口发送数组数据
  * @param	需要发送的数组首地址-也就是数组名
  * @param	需要发送的数组有几个元素
  * @retval	无
  */
void Serial_SendArray(uint8_t *Array, uint16_t Num)
{
	uint8_t i = 0;
	
	for (i = 0; i < Num; i++)
	{
		Serial_SendByte(Array[i]);
	}
}

/**
  * @brief	串口发送字符串
  * @param	需要发送的字符串首元素地址
  * @retval	无
  */
void Serial_SendString(char *String)
{
	uint16_t i = 0;
	
	while (String[i] != '\0')
	{
		Serial_SendByte(String[i]);
		i++;
	}
}

/**
  * @brief	次方函数，x的y次方
  * @param	需要求的次方数
  * @param	y次方
  * @retval	无
  */
uint32_t Serial_NumPow(uint32_t x, uint32_t y)
{
	uint32_t Result = 1;
	
	while (y--)
	{
		Result *= x;
	}
	
	return Result;
}

/**
  * @brief	串口发送数字函数
  * @param	需要发送的数字
  * @param	数字的位数
  * @retval	无
  */
void Serial_SendNum(uint32_t Number, uint16_t Lenth)//12345
{
	uint16_t i = 0;
	
	for (i = 0; i < Lenth; i++)
	{
		Serial_SendByte (Number / Serial_NumPow(10, Lenth-1-i) % 10 + '0');
	}
}

/**
  * @brief	重定义fputc函数到串口printf输出
  * @param	重定义的字符
  * @retval	无
  */
int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

/**
  * @brief	使用自定义格式化打印函数
  * @param	...表示可变参数，不限定个数和类型
  * @retval	无
  */
void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Serial_SendString(String);
}

//uint8_t Serial_GetRxFlag(void)
//{
//	if(Serial_RxFlag == 1)
//	{
//		Serial_RxFlag = 0;
//		return  1;
//	}
//	else
//	{
//		return  0;
//	}
//}

/**
  * @brief	串口接收文本数据包中断函数
  * @param	无
  * @retval	无
  */
void USART1_IRQHandler(void)
{
	static uint8_t Serial_State = 0;
	static uint8_t pRxPacket = 0;
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		Serial_RxData = USART_ReceiveData(USART1);
		
		if(Serial_State == 0 && Serial_RxFlag == 0)
		{
			if(Serial_RxData == '@')
			{
				Serial_State = 1;
				pRxPacket = 0;
			}
		}
		else if(Serial_State == 1)
		{
			if(Serial_RxData == '\r')
			{
				Serial_State = 2;
			}
			else
			{
				Serial_RxPacket[pRxPacket] = Serial_RxData;
				pRxPacket++;
			}
		}
		else if(Serial_State == 2)
		{
			if(Serial_RxData == '\n')
			{
				Serial_State = 0;
				Serial_RxPacket[pRxPacket] = '\0';
				Serial_RxFlag = 1;
			}
		}
			
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}







